(function($) {
	$
			.extend({
				page : function(options) {
					if (!this.instance) {
						var defaults = {
							i18n : true,
							ajax : true,
						}

						if (!options.module) {
							alert("When page init, module must be specified");
							return null;
						}
						
						var that = this;
						var options = $.extend(defaults, options);

						this.instance = {
							module : options.module,
							init : function() {
								if (options.i18n) {
									jQuery.i18n.properties({
										name : [ "site", options.module ],
										path : 'v2/js/i18n/',
										mode : 'map',
										language : $("html").attr("lang"),
										callback : function() {
										}
									});
								}

								if (options.ajax) {
									this.initAjax();
								}
								
								return that.instance;
							},

							initAjax : function() {
								if ($(".ajaxLoading").length == 0) {
									$("body")
											.append(
													"<div class='ajaxLoading'>Loading...</div>");
								}
								$('.ajaxLoading').hide().ajaxStart(function() {
									$(this).show();
								}).ajaxStop(function() {
									$(this).hide();
								});
							}, 
							
							showError: function(message){
								this.showMessage("error", message);
							},
							showWarning: function(message){
								this.showMessage(null, message);
							},
							showSuccess: function(message){
								this.showMessage("success", message);
							}, 
							showInfo: function(message){
								this.showMessage("info", message);
							}, 
							
							showMessage: function(type, message){
								if($("#message").length == 0){
									$("#bar").prepend("<div id='messages' class=''></div>");
								}
								
								$("#message").empty();
								$("#messages").append(this.getMessageTemplate(type, message));
								$("#messages").show();
							},
							
							hideMessage: function(){
								$("#messages").hide();
							},
							
							getMessageTemplate: function(type, message){
								var template = "<div class=\"alert ";
								if(type){
									template += "alert-";
									template += type;
								}
								template += "\">";
								template += "<a class='close' data-dismiss='alert' href='#'>&times;</a>";
								if(type == "error"){
									template += "<b>Error!</b>";
								}else if(type == "success"){
									template += "<b>Success!</b>";
								}else if(type == "info"){
									template += "<b>Infomation!</b>";
								}else{
									template += "<b>Warning!</b>";
								}
								template += "&nbsp;";
								template += message;
								template += "</div>";
								
								return template;
							}
						}
					}

					return this.instance;
				}
			});
})(jQuery);
